<?php

namespace common\libs;

class Curl
{

    /**
     * GET 请求
     * @param string $url
     */
    public static function http_get($url, $lang = 'en-us')
    {
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($oCurl, CURLOPT_TIMEOUT,6); 
        curl_setopt($oCurl, CURLOPT_ENCODING, 'gzip, deflate');

        curl_setopt($oCurl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:40.0) Gecko/20100101 Firefox/40.0');

        if ($lang == 'zh-cn') {
            curl_setopt($oCurl, CURLOPT_HTTPHEADER, ['Accept-Language: zh-CN,zh;q=0.8']);
        }
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);

// 		if($lang == 'zh-cn'){
// 			error_log(json_encode($aStatus));
// 			error_log($sContent);
// 		}
        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
        } else {
            return false;
        }
    }

    public static function cURL($url, $ref = null, $postData = null)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0');
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    /**
     * 使用代理请求
     * @param $url
     * @return mixed
     */
    public static function steamprice_proxy_curl($url, $lang = 'en-us', $is_rand = true)
    {
        $proxyServer = 'http://zproxy.luminati.io:22225';

        $proxyUser = "lum-customer-hl_60cf9469-zone-steamprice";
        $proxyPass = "6rgv4x3wtd2c";
        $ch = curl_init();
        if($is_rand){
            curl_setopt($ch, CURLOPT_URL, $url . '&' . rand(1, 1000));
        }else{
            curl_setopt($ch, CURLOPT_URL, $url);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_PROXY, $proxyServer);
        curl_setopt($ch, CURLOPT_PROXYUSERPWD, "{$proxyUser}:{$proxyPass}"); //http代理认证帐号，username:password的格式

        if ($lang == 'zh-cn') {
            curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept-Language: zh-CN,zh;q=0.8']);
        }

        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    /**
     * POST 请求
     * @param string $url
     * @param array $param
     * @param boolean $post_file 是否文件上传
     * @return string content
     */
    public static function http_post($url, $param, $post_file = false)
    {
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        if (is_string($param) || $post_file) {
            $strPOST = $param;
        } else {
            $aPOST = array();
            foreach ($param as $key => $val) {
                $aPOST[] = $key . "=" . urlencode($val);
            }
            $strPOST = join("&", $aPOST);
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($oCurl, CURLOPT_POST, true);
        curl_setopt($oCurl, CURLOPT_HTTPHEADER, ['Accept-Language: zh-CN,zh;q=0.8']);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
        } else {
            return false;
        }
    }

    /**
     * POST 请求
     * @param string $url
     * @param array $param
     * @param boolean $post_file 是否文件上传
     * @return string content
     */
    public static function http_post_for_send_trade($url, $param, $post_file = false)
    {
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        if (is_string($param) || $post_file) {
            $strPOST = $param;
        } else {
            $aPOST = array();
            foreach ($param as $key => $val) {
                $aPOST[] = $key . "=" . urlencode($val);
            }
            $strPOST = join("&", $aPOST);
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($oCurl, CURLOPT_POST, true);
        curl_setopt($oCurl, CURLOPT_TIMEOUT,10);
        curl_setopt($oCurl, CURLOPT_HTTPHEADER, ['Accept-Language: zh-CN,zh;q=0.8']);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
        } else {
            return false;
        }
    }

    /**
     * 使用代理请求
     * @param $url
     * @return mixed
     */
    public static function sell66_user_inv_proxy_curl($url, $lang = 'en-us')
    {
        $proxyServer = 'http://127.0.0.1:24001';

        $proxyUser = "lum-customer-hl_60cf9469-zone-zone1";
        $proxyPass = "z5x3716ihkfv";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url . '&' . rand(1, 1000));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_PROXY, $proxyServer);
        curl_setopt($ch, CURLOPT_PROXYUSERPWD, "{$proxyUser}:{$proxyPass}"); //http代理认证帐号，username:password的格式
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        if ($lang == 'zh-cn') {
            curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept-Language: zh-CN,zh;q=0.8']);
        }

        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    public static function abuyunProyCurl($url, $lang = 'en-us')
    {
        // 代理服务器
        $proxyServer = "http://zproxy.luminati.io:22225";

        // 隧道身份信息
        $proxyUser = "lum-customer-hl_60cf9469-zone-zone1";
        $proxyPass = "z5x3716ihkfv";
        
        $ch = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        // 设置代理服务器
        curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
        curl_setopt($ch, CURLOPT_PROXY, $proxyServer);

        // 设置隧道验证信息
        curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_PROXYUSERPWD, "{$proxyUser}:{$proxyPass}");

        curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');

        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:40.0) Gecko/20100101 Firefox/40.0');

        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);

        if ($lang == 'zh-cn') {
            curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept-Language: zh-CN,zh;q=0.8']);
        }

        $result = curl_exec($ch);
        $aStatus = curl_getinfo($ch);
        curl_close($ch);
        if (intval($aStatus["http_code"]) == 429) {
            return $aStatus["http_code"];
        }
        if (intval($aStatus["http_code"]) == 200) {
            return $result;
        } else {
            return false;
        }
    }
}